/*	kd:
	Binds are shown and set in the ring as well. That means we have to output
	this stuff to the user. */

struct Gcs_BindSprite {
	const fade_step = 1.0 / 5;
	const base_color = Font.CR_ICE;
	const select_color = Font.CR_SAPPHIRE;
	const ver_offset = 0.05;
	
	enum anim_state {
		ANIM_OUT = 0,
		ANIM_IN,
		ANIM_NONE
	}
	
	Gcs_LinearFades m_fade;
	String m_bind_str     [Gc_Binds.bind_keys];
	float  m_bind_offset  [Gc_Binds.bind_keys];
	
	anim_state m_state  [Gc_Binds.bind_keys];
	uint m_item         [Gc_Binds.bind_keys];
	uint m_pending_item [Gc_Binds.bind_keys];
	bool m_dont_draw    [Gc_Binds.bind_keys];
	uint m_item_count;
	
	void Init () {
		m_fade.ClearAllReserve(Gc_Binds.bind_keys);
	}
	
	void FillFromShop (Actor user, Gc_Shop_Inv inv) {
		if(user == NULL || !(user is 'Guncaster')) {
			return;
		}
		
		Guncaster mo = Guncaster(user);
		
		m_item_count = inv.m_size;
		for(uint i = Gc_Binds.bind_keys; 0 <= --i;) {
			int item_i = inv.FindItem(mo.BoundItem(i));
			m_item         [i] = item_i;
			m_pending_item [i] = m_item [i];
			
			if(item_i == -1) {
				m_dont_draw [i] = true;
			}
			
			else {
				MakeString(i);
				m_dont_draw [i] = false;
			}
		}
	}
	
	void AllIn () {
		for(uint i = Gc_Binds.bind_keys; 0 <= --i;) {
			m_fade.FadeContinue(i, 1, fade_step);
		}
	}
	
	void AllOut () {
		for(uint i = Gc_Binds.bind_keys; 0 <= --i;) {
			m_fade.FadeContinue(i, 0, fade_step);
		}
	}
	
	void MakeString (uint bind) {
		if(Gc_Binds.bind_keys <= bind) {
			return;
		}
		
		// todo: get the key name... somehow. lol zs
		String key_str = StringTable.Localize("$BIND_KEY");
		String bind_str = String.Format("%s: %i", key_str, bind);
		
		m_bind_offset [bind] = 0.5 * smallfont.StringWidth(bind_str);
		m_bind_str    [bind] = bind_str;
	}
	
	void StepAnim () {
		m_fade.StepAllFaders();
		
		for(uint i = Gc_Binds.bind_keys; 0 <= --i;) {
			bool fade_done = 1 <= m_fade.Interpol(i);
			
			if(fade_done && m_state [i] == ANIM_OUT) {
				m_state [i] = ANIM_IN;
				m_item  [i] = m_pending_item [i];
				m_fade.FadeContinue(i, 1, fade_step);
				MakeString(i);
			}
			
			else if(m_state [i] == ANIM_IN) {
				m_state [i] = ANIM_NONE;
			}
		}
	}
	
	void RebindAnim (uint bind, uint item) {
		if(Gc_Binds.bind_keys <= bind || m_item_count <= item) {
			return;
		}
		
		if(m_dont_draw [bind]) {
			m_item         [bind] = item;
			m_pending_item [bind] = item;
			m_dont_draw    [bind] = false;
			m_state        [bind] = ANIM_IN;
			m_fade.Fade(bind, 0, 1, fade_step);
		}
		
		else {
			m_pending_item [bind] = item;
		}
		
		if(m_state [bind] != ANIM_OUT) {
			m_fade.FadeContinue(bind, 0, fade_step);
			m_state [bind] = ANIM_OUT;
		}
	}
	
	ui void Draw (
		RenderEvent      event,
		Gcs_InvLayout    layout,
		Gcs_LinearFades  select_fade,
		float            ui_alpha = 0.333,
		float            ui_scale = 1.666) const {
		
		float aspect = Screen.GetAspectRatio();
		float height = 720 / ui_scale;
		// float width = height;
		float width  = aspect * height;
		float t      = event.fractic;
		float t_inv  = 1 - t;
		
		for(uint i = Gc_Binds.bind_keys; 0 <= --i;) {
			if(m_dont_draw [i]) {
				continue;
			}
			
			// Here, it doesn't really matter to me if the alpha values add up
			// correctly to the desired alpha, because it's just a selection
			// effect.
			uint k = m_item [i];
			
			float base_alpha = m_fade.Current(i);
			float sel_alpha  = base_alpha * select_fade.Current(k);
			float x = t_inv * layout.m_old_pos_x [k] + t * layout.m_pos_x [k];
			float y = t_inv * layout.m_old_pos_y [k] + t * layout.m_pos_y [k];
			
			x = layout.center_x - x / aspect;
			y = layout.center_y + y + ver_offset;
			
			Screen.DrawText(smallfont, base_color,
				x * width - m_bind_offset [i],
				y * height,
				m_bind_str [i],
				DTA_ALPHA, base_alpha,
				DTA_KEEPRATIO, true,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
			
			Screen.DrawText(smallfont, select_color,
				x * width - m_bind_offset [i],
				y * height,
				m_bind_str [i],
				DTA_ALPHA, sel_alpha,
				DTA_KEEPRATIO, true,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height);
		}
	}
}